//
//  SecondViewController.m
//  League
//
//  Created by JinHvung on 2018/2/11.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#define kSegmentH 40
#import "SecondViewController.h"
#import "XFSegmentView.h"
#import "RCConversationsViewController.h"

@interface SecondViewController ()<XFSegmentViewDelegate, RCIMClientReceiveMessageDelegate>

@property(strong , nonatomic)XFSegmentView *segView;
@property(strong , nonatomic)NSMutableArray *titleArr;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置IM消息监听
    [[RCIMClient sharedRCIMClient] setReceiveMessageDelegate:self object:nil];
    
    [self.view addSubview:self.segView];
    [self setupChildVCs];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.tabBarController.tabBar.hidden = NO;
    
    [self childViewLoadConversion];
    [self loadUnReadMessageCount];
}

#pragma mark - XFSegmentView
-(XFSegmentView *)segView
{
    if(!_segView){
        XFSegmentView *segView=[[XFSegmentView alloc]initWithFrame:Frame(0, 0, SCREEN_WIDTH, kSegmentH)];
        segView.delegate = self;
        
        NSString *lan = [InternationalControl userLanguage];
        if([lan isEqualToString:@"en"]){
            
            segView.titles =
            @[@"Match",
              @"Expired", @"Friends",@"Events",@"Groups"];
        }
        else
        {
            segView.titles =
            @[@"相配",
              @"过期", @"好友",@"聚一聚",@"聊一聊"];
        }
        segView.titleFont = [UIFont fontWithName:kFontName size:14];
        
        _segView = segView;
    }
    return _segView;
}
-(void)segmentView:(XFSegmentView *)segmentView didSelectIndex:(NSInteger)index {
    NSLog(@"segmentView-didSelectIndex:%@", @(index));
    
    [self showChildViewWithIndex:index];
    [self childViewLoadConversion];
}
- (void)showChildViewWithIndex:(NSInteger)index {
    for (NSInteger tabIndex = 0; tabIndex < self.childViewControllers.count; tabIndex ++) {
        RCConversationsViewController *VC = self.childViewControllers[tabIndex];
        VC.tableView.hidden = index == tabIndex ? NO : YES;
    }
}

/**
 当前显示tableView获取数据
 */
- (void)childViewLoadConversion {
    for (NSInteger tabIndex = 0; tabIndex < self.childViewControllers.count; tabIndex ++) {
        RCConversationsViewController *VC = self.childViewControllers[tabIndex];
        if(VC.tableView.hidden == NO){
            [VC loadData];
        }
    }
}
#pragma mark - setupChildVCs
- (void)setupChildVCs {
    CGRect tabFrame = CGRectMake(0, kSegmentH, kScreenWidth, kScreenHeight - (kNaviHeight) - (kSegmentH) - (kTabbarHeight));
    for (NSInteger index = 0; index < 5; index ++) {
        RCConversationsViewController *VC = [[RCConversationsViewController alloc] init];
        VC.tabFrame = tabFrame;
        VC.tableView.hidden = YES;
        VC.index = index;
        [self.view addSubview:VC.tableView];
        [self addChildViewController:VC];
    }
    
    [self showChildViewWithIndex:0];
}

#pragma mark - loadUnReadMessageCount
- (void)loadUnReadMessageCount {
    [UIApplication sharedApplication].applicationIconBadgeNumber =  [[RCIMClient sharedRCIMClient] getTotalUnreadCount];
    
    UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:1];
    if([[RCIMClient sharedRCIMClient] getTotalUnreadCount]> 0){
        item.badgeValue = [NSString stringWithFormat:@"%@",@([[RCIMClient sharedRCIMClient] getTotalUnreadCount])];
        if (@available(iOS 10.0, *)) {
            item.badgeColor = kAppColor;
        } else {
            // Fallback on earlier versions
            
        }
    }
    else
    {
        item.badgeValue = [NSString stringWithFormat:@"%@",@""];
        if (@available(iOS 10.0, *)) {
            item.badgeColor = [UIColor clearColor];
        } else {
            // Fallback on earlier versions
        }
    }
}

#pragma mark - RCIMClientReceiveMessageDelegate
- (void)onReceived:(RCMessage *)message left:(int)nLeft object:(id)object {
    NSLog(@"onReceived-message: -nLeft:%@", message, @(nLeft));
    
    [self childViewLoadConversion];
    [self loadUnReadMessageCount];
}
@end
